package com.winexpress.support;

import com.winexpress.entity.Enquiry;
import com.winexpress.session.support.EnquirySessionBean;
import com.winexpress.util.EmailManager;
import java.util.List;
import javax.ejb.EJB;
import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import com.winexpress.helper.AESEncryptor;
import com.winexpress.helper.Config;


@WebService(serviceName = "EnquiryManagementWS")
public class EnquiryManagementWS {

    @EJB
    private EnquirySessionBean enquirySessionBean;

    @WebMethod(operationName = "submitEnquiry")
    public String submitEnquiry(@WebParam(name = "enquiry") EnquiryToObject enquiry) {
        Long userID = enquiry.getUid();
        String subject=enquiry.getSubject();
        String category=enquiry.getCategory();
        String content = enquiry.getContent();
        String email=enquiry.getEmail();
        Long enquiryID = enquirySessionBean.createEnquiry(userID, subject, category, content);
        String message = "Thank you for your enquiry to Winexpress.com! Our admin will contact you soon! Your enquiry ID is:" + String.valueOf(enquiryID);
        EmailManager emailManager = new EmailManager(email, "Enquiry Created-winexpress", message);
        Thread thread = new Thread(emailManager);
        thread.start();
        return String.valueOf(enquiryID);

    }

    @WebMethod(operationName = "sendGroupEmail")
    public void sendGroupEmail(@WebParam(name = "enquiry") EnquiryToObject enquiry) throws Exception {
        List<String> emailList = enquirySessionBean.adminEmailList();
        Long enquiryid=enquiry.getEnquiryId();
        Enquiry retrievedEnquiry = enquirySessionBean.retrieveEnquiry(enquiryid);
        String message = "Customer " + String.valueOf(enquiry.getUid()) + " has created a new enquiry. Enquiry ID: " + String.valueOf(enquiryid) + " Please login and check. thank you! \n\n"
                + "Subject: " + retrievedEnquiry.getSubject() + "\n"
                + "Category: " + retrievedEnquiry.getCategory() + "\n"
                + "Content: " + retrievedEnquiry.getContent();
       
        for (int i = 0; i < emailList.size(); i++) {            
            
            String decrypted=AESEncryptor.decrypt(Config.seed, emailList.get(i));
            System.out.println(decrypted);
            EmailManager emailManager = new EmailManager(decrypted, "New Enquiry Created-winexpress", message);
            Thread thread = new Thread(emailManager);
            thread.start();
        }

    }

    @WebMethod(operationName = "customerViewEnquiry")
    public List<Enquiry> customerViewEnquiry(@WebParam(name = "enquiry") EnquiryToObject enquiry) {
        String uid=String.valueOf(enquiry.getUid());
        List<Enquiry> custenq = enquirySessionBean.listCustomerEnquiry(uid);
        return custenq;
    }
     @WebMethod(operationName = "customerViewEnquiryPending")
    public List<Enquiry> customerViewEnquiryPending(@WebParam(name = "enquiry") EnquiryToObject enquiry) {
        String uid=String.valueOf(enquiry.getUid());
        List<Enquiry> custenq = enquirySessionBean.listCustomerEnquiryPending(uid);
        return custenq;
    }

    @WebMethod(operationName = "listPendingEnquiry")
    public List<Enquiry> listPendingEnquiry() {
        List<Enquiry> pending = enquirySessionBean.listPendingEnquiry();
        return pending;
    }

    @WebMethod(operationName = "listRespondedEnquiry")
    public List<Enquiry> listRespondedEnquiry() {
        List<Enquiry> responded = enquirySessionBean.listRespondedEnquiry();
        return responded;
    }

    @WebMethod(operationName = "listAllEnquiry")
    public List<Enquiry> listAllEnquiry() {
        List<Enquiry> all = enquirySessionBean.listAllEnquiry();
        return all;
    }

    @WebMethod(operationName = "retrieveEnquiryByEid")
    public Enquiry retrieveEnquiryByEid(@WebParam(name = "enquiry") EnquiryToObject enquiry) {
        Long eid=enquiry.getEnquiryId();
        Enquiry enq = enquirySessionBean.retrieveEnquiry(eid);
        return enq;
    }

   
    @WebMethod(operationName = "replyEnquiry")
    public Enquiry replyEnquiry(@WebParam(name = "enquiry") EnquiryToObject enquiry) {
        Long eid=enquiry.getEnquiryId();
        Long adminid=enquiry.getAdminid();
        String reply=enquiry.getReply();
        
        Enquiry enq = enquirySessionBean.setReply(eid, adminid, reply);
        return enq;
    }

  
    @WebMethod(operationName = "replyEnquiryEmail")    
    public void replyEnquiryEmail(@WebParam(name = "enquiry") EnquiryToObject enq) throws Exception {
        Long eid=enq.getEnquiryId();
        Enquiry enquiry = enquirySessionBean.retrieveEnquiry(eid);
        String message = "Dear Customer,\n " + "Your enquiry to winexpress.com has been responded by one of our customer service staff. \n\n"
                + "Enquiry ID: " + String.valueOf(eid) + "\n"
                + "Date Created:" + enquiry.getDateCreated() + "\n"
                + "Customer Service Staff ID: " + enquiry.getAdmin().getId() + "\n"
                + "Subject: " + enquiry.getSubject() + "\n"
                + "Category: " + enquiry.getCategory() + "\n"
                + "Content: " + enquiry.getContent() + "\n\n"
                + "Reply: " + enquiry.getReply() + "\n\n\n"
                + "If you have any further enquires, please create a new enquiry or contact our customer service staff directly. Thank you!" + "\n\n"
                + "Winexpress.com";

        EmailManager emailManager = new EmailManager(enquiry.getCustomer().getEmail(), "Your Enquiry Has Benn Responded", message);
        Thread thread = new Thread(emailManager);
        thread.start();
        ;
    }


}
